package hoang.hut.app.utility;

import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * Class use to manager shared preference use to in application
 * 
 * 
 */
public class SharePreferenceUtility {

	private SharedPreferences preferences = null;
	private static SharePreferenceUtility instance = null;

	@Deprecated
	private SharePreferenceUtility() {
	}

	public static SharePreferenceUtility getInstance(Context context) {
		if (instance == null) {
			instance = new SharePreferenceUtility();
			instance.preferences = PreferenceManager
					.getDefaultSharedPreferences(context);
		}
		return instance;
	}

	/**
	 * Method used to set object value to preference editor
	 * 
	 * @param key
	 *            : The name of the preference to modify.
	 * @param object
	 *            : The new value for the preference.
	 */
	public void putWithCommit(String key, Object object) {
		if (object instanceof Integer) {
			preferences.edit().putInt(key, ((Integer) object)).commit();
		} else if (object instanceof String) {
			preferences.edit().putString(key, object.toString()).commit();
		} else if (object instanceof Long) {
			preferences.edit().putLong(key, ((Long) object)).commit();
		} else if (object instanceof Float) {
			preferences.edit().putFloat(key, ((Float) object)).commit();
		} else if (object instanceof Boolean) {
			preferences.edit().putBoolean(key, ((Boolean) object)).commit();
		}
	}
	/**
	 * Method used to get object value from preference
	 * 
	 * @param key
	 *            : The name of the preference to modify.
	 * @return : Returns the preference value if it exists, otherwise return
	 *         null
	 */
	@Deprecated
	public Object get(String key) {
		Map<String, ?> map = preferences.getAll();
		for (Map.Entry<String, ?> e : map.entrySet()) {
			if (e.getKey().equalsIgnoreCase(key)) {
				return e.getValue();
			}
		}
		return null;
	}

}
